<?php
class Upload {
    public $filepath = './upload'; //上传目录
    private $tmpPath;  //PHP文件临时目录
    private $blobNum; //第几个文件块
    private $totalBlobNum; //文件块总数
    private $fileName; //文件名
	private $errors = array();
 
	public function __construct(){
		
	}
    public function init($tmpPath,$blobNum,$totalBlobNum,$fileName){
        $this->tmpPath =  $tmpPath;
        $this->blobNum =  $blobNum;
        $this->totalBlobNum =  $totalBlobNum;
        $this->fileName =  $fileName;

    }
    public function run(){
		$this->moveFile();
        $this->fileMerge();
	}
	
    //判断是否是最后一块，如果是则进行文件合成并且删除文件块
    private function fileMerge(){
		
        if($this->blobNum == $this->totalBlobNum){
			
            $blob = '';
            for($i=1; $i<= $this->totalBlobNum; $i++){
                $blob = file_get_contents($this->filepath.'/'. $this->fileName.'__'.$i);
                file_put_contents($this->filepath.'/'. $this->fileName,$blob,FILE_APPEND);
            }
			
            $this->deleteFileBlob();
        }
    }
     
   //删除文件块
    private function deleteFileBlob(){
        for($i=1; $i<= $this->totalBlobNum; $i++){
            @unlink($this->filepath.'/'. $this->fileName.'__'.$i);
        }
    }
     
    //移动文件
    private function moveFile(){
        $this->touchDir();
        $filename = $this->filepath.'/'. $this->fileName.'__'.$this->blobNum;
        move_uploaded_file($this->tmpPath,$filename);
    }
     
    //API返回数据
    public function apiReturn(){
		$data = array();
        if($this->blobNum == $this->totalBlobNum){
                if(file_exists($this->filepath.'/'. $this->fileName)){
                    $data['code'] = 0;
                    $data['msg'] = 'success';
                    //$data['file_path'] = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).str_replace('.','',$this->filepath).'/'. $this->fileName;
					$data['file_path'] = 'tmpfile|'.$this->fileName;
                }
        }else{
                if(file_exists($this->filepath.'/'. $this->fileName.'__'.$this->blobNum)){
                    $data['code'] = 1;
                    $data['msg'] = 'waiting for all';
                    $data['file_path'] = '';
                }
        }
		return $data;
    }
     
    //建立上传文件夹
    private function touchDir(){
        if(!file_exists($this->filepath)){
            return mkdir($this->filepath);
        }
    }
	
	
	public function uploadfile_cl($file,$dir,$fext=''){
		
		if(strpos($file,'|')!==false){
			$fg = explode('|',$file);
			if(count($fg)>=2 && $fg[0]=='tmpfile'){
				$time = time();
				$ls_fg = explode('.',$fg[1]);
				$ext = end($ls_fg);
				if(!empty($fext)){
					$ls_fg1 = explode('|',$fext);
					
					if(!in_array($ext,$ls_fg1)){
						$this->addError('不允许的文件类型');
						return false;
					}
				}
				
				if(!is_dir($dir)){
					@mkdir($dir);
				}
				
				$filepath = '';
				while(true){
					$filepath = $dir.'/'.$time.'_'.rand(1000,9999).'.'.$ext;
					if(!file_exists($filepath)){
						break;
					}
				}

				$tmpdir = FCPATH . 'res/tmp/';
				$source_file = $tmpdir.$fg[1];
				if(!file_exists($source_file)){
					$this->addError('源文件不存在');
					return false;
				}
				
				if(!copy($source_file,FCPATH.$filepath)){
					$this->addError('文件移动错误');
					return false;
				}
				@unlink($source_file);
				
				return $filepath;
			}
		}

		return $file;
	}
	
	function addError($msg){
		$this->errors[] = $msg;
		
	}
	function getError($index=0){
		if(isset($this->errors[$index])){
			return $this->errors[$index];
		}
		
		return $this->errors;
	}
}
